/**
 * 提现
 * @author  Jello
 * @create  2018/10/19 10:58
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var i = 0;
var userid_sign = "";
var balance = "";
var sessionStorage = window.sessionStorage;

$(function() {
	userid_sign = sessionStorage.getItem("userid_sign");
	if(!userid_sign) {
		userid_sign = sessionStorage.getItem("openid");
	}

	var bankName = sessionStorage.getItem("with_bankname"); //开户行名称
	var bankNumber = sessionStorage.getItem("with_banknumber"); //尾号
	var cardType = sessionStorage.getItem("with_cardtype"); //卡类型
	$("#bankName").html(bankName);
	$("#bankNum").html(bankNumber);
	$("#cardType").html(cardType);

	//获取个人资料
	var param = {};
	param.userid_sign = userid_sign;
	getUserMaterial(param, function(data) {
		balance = toDecimal(data.money);
		//余额
		$("#balance").html(balance);
	});

	//全部提现
	$("#total").click(function() {
		$("#money_value").val($("#balance").html());
		$(".sta_kt").show();
		$(".sta_cc").hide();
	})

	//提现确定
	$("#withdraw_btn").click(function() {
		var money = $("#money_value").val();
		var balance = $("#balance").html();
		$("#tipMoney").html(money);
		var reg = /^([1-9]\d{0,9}|0)([.]?|(\.\d{1,2})?)$/;
		if(money == "" || money == null) {
			mui.toast("请输入提现金额");
			return;
		} else if(money == 0) {
			mui.toast("请输入正确的金额");
			return;
		} else if(!reg.test(money)) {
			mui.toast("请输入正确的金额");
			return;
		} else if(eval(money) > eval(balance)) {
			return;
		} else if(eval(money) < 20.00) {
			mui.toast("提现金额最低20元");
			return;
		} else {
			$(".tx_bck").show();
		}
	})

	//关闭浮动
	$("#close").click(function() {
		$(".tx_bck").hide();
		$(".pop_mpb .p_mlist").removeClass("mmdd");
		$(".pop_mpb .p_mlist").attr("withpwd", "");
		i = 0;
	});

	//数字键盘显示隐藏
	$(".xiaq_tb").click(function() {
		$(".numb_box").slideUp(500);
	});
	$(".pop_mpb").click(function() {
		$(".numb_box").slideDown(500);
	});

	//删除
	$(".nub_ggg li .zf_del").click(function() {
		if(i > 0) {
			i--
			$(".pop_mpb .p_mlist").eq(i).removeClass("mmdd");
			$(".pop_mpb .p_mlist").eq(i).attr("withpwd", "");
		}
	});

	//提现密码校验
	$(".nub_ggg li .zf_num").click(function() {
		if(i < 6) {
			$(".pop_mpb .p_mlist").eq(i).addClass("mmdd");
			$(".pop_mpb .p_mlist").eq(i).attr("withpwd", $(this).text());
			i++;
			if(i == 6) {
				//判断本地存储密码次数为3次并且cookie中存储的值为null时,就把本地存储的次数清除(cookie10分钟失效后要把本地存储的密码次数删除)
				if(localStorage.getItem("sess_time_local") == 3 && $.cookie("sess_time_local") == null) {
					localStorage.removeItem("sess_time_local");
				}
				var sess_time = localStorage.getItem("sess_time_local"); //当前密码错误次数就等于本地存储的次数,牵扯到提现密码的页面都会记录并且每次叠加,直接赋值
				setTimeout(function() {
					var withpwd = "";
					$(".pop_mpb .p_mlist").each(function() { //获取输入的密码
						withpwd += $(this).attr("withpwd");
					});
					if($.cookie("sess_time_local") == 3) { //密码错误次数过多
						$(".esc_divbox2").show();
						$(".esc_box2").show();
						return;
					} else {
						var money = $("#money_value").val();
						//提现接口上送
						$.ajax({
							type: "post",
							url: serverUrl + "/billrecord/withdraw_deposit?sgin=" + userid_sign + "&drawPwd=" + hex_md5(hex_md5(withpwd) + "lanpin123.com"),
							data: "{\"bankName\":\"" + bankName + "\",\"bankNum\":\"" + bankNumber + "\",\"money\":\"" + money + "\"}",
							async: false,
							cache: false,
							contentType: "application/json; charset=utf-8",
							dataType: "json",
							crossDomain: true,
							timeout: 15000,
							success: function(data) {
								$(".tx_bck").hide();
								var resultObj = eval(data);
								//								console.log("提现接口：" + JSON.stringify(resultObj));
								if(resultObj.code == 0 && resultObj.data == 1) {
									mui.toast("提现申请成功");
									setTimeout(function() {
										if(sessionStorage.getItem("wallet_sign") == 001166) {
											window.location.href = "../../wallet.html";
										} else {
											window.location.href = "../../app/personal/personal.html";
										}
									}, 800);
								} else {
									//身份验证失败,记录错误提现密码次数
									sess_time++;
									localStorage.setItem("sess_time_local", sess_time);
									if(sess_time == 3) { //错误次数达到3次
										$(".esc_divbox2").show();
										$(".esc_box2").show();
										var date = new Date();
										date.setTime(date.getTime() + 60 * 1000 * 10); //过期为10分钟
										$.cookie("sess_time_local", sess_time, { //存储次数cookie
											expires: date,
											path: '/bluehired'
										});
									} else { //密码错误次数提醒
										$("#local_time").html((3 - sess_time));
										$(".esc_box").show();
										$(".esc_divbox").show();
									}
								}
							},
							error: function(XMLHttpRequest, status, errorThrown) {
								if(status == 'timeout') {
									mui.toast("请求超时");
								} else {
									mui.toast("请求网络服务数据失败");
								}
								return;
							}
						});
					}
				}, 100);
			}
		}
	})

	//提现密码错误，请重试遮罩层关闭
	//	$(".esc_divbox").click(function() {
	//		$(".esc_box").hide();
	//		$(".esc_divbox").hide();
	//		emptyPwd();
	//	})

	//提现密码错误，请重试
	$("#retrybtn").click(function() {
		$(".tx_bck").show();
		$(".esc_box").hide();
		$(".esc_divbox").hide();
		$(".pop_mpb .p_mlist").removeClass("mmdd");
		$(".pop_mpb .p_mlist").attr("withpwd", "");
		i = 0;
	})

	//提现密码次数过多事件
	$(".tm_ckdeil2").click(function() {
		if(sessionStorage.getItem("wallet_sign") == 001166) {
			window.location.href = "../../wallet.html";
		} else {
			window.location.href = "../../app/personal/personal.html";
		}
	})

	//忘记密码
	$("#forget_withdrawpwd").click(function() {
		window.location.href = "../../app/bank/withdrawpwd_updatecode.html";
	})

})

/**
 * 提现金额oninput事件
 */
function checkMoney() {
	var money = parseFloat($("#money_value").val().trim());
	var balance = parseFloat($("#balance").html());
	if(eval(money) > eval(balance)) {
		$(".sta_kt").hide();
		$(".sta_cc").show();
		$("#close_img").css("display", "block");
	} else {
		$(".sta_kt").show();
		$(".sta_cc").hide();
		$("#close_img").css("display", "none");
	}
}

/**
 * 提现金额过大出现X
 */
document.getElementById("close_img").addEventListener('tap', function() {
	$("#money_value").val("");
	checkMoney();
}, false);

//清空密码
function emptyPwd() {
	$(".pop_mpb .p_mlist").removeClass("mmdd");
	$(".pop_mpb .p_mlist").attr("withpwd", "");
	i = 0;
}

/**
 * 提现返回
 */
function withdrawBack() {
	if(sessionStorage.getItem("wallet_sign") == 001166) {
		window.location.href = "../../wallet.html";
	} else {
		window.location.href = "../../app/personal/personal.html";
	}
}